{include file="public/head"/}
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">模型列表</div>
        <div class="layui-card-body">
            <table class="layui-table" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="status" {{d.status == 1 ? 'checked' : ''}}>
</script>
<script type="text/html" id="action">
	<button data-id="{{d.id}}" class="layui-btn layui-btn-xs" lay-event="fields" lay-text="{{d.title}}字段" title="{{d.title}}字段管理"><i class="layui-icon layui-icon-table"></i>字段管理</button>
    <button data-id="{{d.id}}" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>
    <button data-id="{{d.id}}" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
</script>
<script type="text/html" id="toolbarModule">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加模型</button>
    </div>
</script>
<script>
    layui.config({
        base: '__LAYDMIN__/',{if config('app.app_debug')}version: true{/if}
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','form'],function(){
		var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form;
		 var tableLoadDiv = window.top.layer.load(0,{shade:[0.15, '#000']}); //加载层,table.render完成时需要关闭
		//全部数据
		tableIn = table.render({
			elem: '#list',
			url: '{:url(SYS_PATH.'/'.CONTROLLER.'/index')}',
			method: 'post',
			page:true,
			toolbar: '#toolbarModule',
			defaultToolbar:'',
			autoSort: false,
			cols: [[
				{field: 'id', title: 'ID', width:60, fixed: true,sort:true},
				{field: 'title', title: '模型名称', width: 120},
				{field: 'name', title: '表名', width: 100,sort:true},
				{field: 'description', title: '详细介绍', width: 180},
				{field: 'status', align: 'center',title: '状态', width: 120,toolbar: '#status'},
				{width: 300, align: 'center', toolbar: '#action'}
			]],
			done:function (res) {   //返回数据执行回调函数
							window.top.layer.close(tableLoadDiv);    //返回数据关闭loading
			}			
		});
		
		//头工具栏事件
		table.on('toolbar(list)', function(obj){
			if(obj.event=='add'){
				layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/add')}",{type:'add'});
			}
		});	
		
		//监听工具条
		table.on('tool(list)', function(obj){
			var data = obj.data;
			var id = data.id;
			var title = data.title;
			if(obj.event === 'del'){
				var uri="{:url(SYS_PATH.'/'.CONTROLLER.'/doDelete')}"
				layui.doDelete(uri,"您确定要删除该模型吗？",id,obj);//单个删除接口				
			} else if(obj.event === 'edit'){
				layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/edit')}?id="+id,{type:'edit'});
			} else if(obj.event === 'fields'){
				var uri="{:url(SYS_PATH.'/field/index')}?mid="+id;
				layui.layerOpen(uri,{type:'fields'},'','',title);
			}
		});	
		
		//改变模型状态
		form.on('switch(status)', function(obj){
			var id = this.value;
			var status = obj.elem.checked===true?1:0;
			admin.req({
				url: '{:url(SYS_PATH.'/'.CONTROLLER.'/doStatus')}'
				, data: {'id':id,'status':status}
				, type:'post'
				, success: function (res) {
					if(res.code==0){
						window.top.layer.msg(res.msg,{time:1000,icon:1,shade:[0.5,'#000']});
					}else{
						if(obj.elem.checked==true){
							$(obj.elem).prop('checked',false);
						}else{
							$(obj.elem).prop('checked',true);
						}
						form.render();
					}
				}
			});
		});		
		
	});
</script>
</body>
</html>